package me.lc.linklinkchrist.util;

import me.lc.linklinkchrist.MainActivity;
import me.lc.linklinkchrist.core.GameManager;
import me.lc.linklinkchrist.core.StageManager;
import me.lc.linklinkchrist.scene.GameScene;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;

import com.admogo.AdMogoLayout;
import com.admogo.AdMogoListener;
import com.wiyun.engine.utils.ResolutionIndependent;

public class ADHelper implements AdMogoListener {
	private static final String mogoID = "31cd85b3d94347efa0300bbb4741ec0b";
	private static ADHelper Instance = new ADHelper();
	private static AdMogoLayout adMogoLayoutCode;
	private static Object syncObj = new Object();
	private static boolean isShowed = false;

	private ADHelper() {
	}

	public static void initAD() {
		MainActivity.instance.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				synchronized (syncObj) {
					if (AppControl.isShowAd() && !isShowed) {
						isShowed = true;
						adMogoLayoutCode = new AdMogoLayout(
								MainActivity.instance, mogoID);
						FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
								FrameLayout.LayoutParams.FILL_PARENT,
								(int) ResolutionIndependent.resolveDp(50));
						// 设置广告出现的位置(悬浮于顶)
						params.gravity = Gravity.TOP;
						params.bottomMargin = 0;
						params.leftMargin = 0;
						// 添加广告状态监听
						adMogoLayoutCode.setAdMogoListener(ADHelper.Instance);
						MainActivity.instance.addContentView(adMogoLayoutCode,
								params);
						hideAd();
					}
				}
			}
		});
	}

	public static void showAd() {
		if (adMogoLayoutCode != null) {
			MainActivity.instance.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					adMogoLayoutCode.setVisibility(View.VISIBLE);
				}
			});
		}
	}

	public static void hideAd() {
		if (adMogoLayoutCode != null) {
			MainActivity.instance.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					adMogoLayoutCode.setVisibility(View.INVISIBLE);
				}
			});
		}
	}

	@Override
	public void onClickAd() {

		// 暂停时点广告有奖励
		if (GameManager.instance.isPaused()) {
			// 增加2000金币
			StageManager.instance.goldCount += 2000;
			StageManager.instance.saveGold();
			GameScene.make().updateGold();
		} else {
			// 点击广告暂停游戏
			GameScene.make().pauseGame();
		}
	}

	@Override
	public void onCloseMogoDialog() {

	}

	@Override
	public void onFailedReceiveAd() {

	}

	@Override
	public void onReceiveAd() {

	}

	@Override
	public void onCloseAd() {
		// TODO Auto-generated method stub

	}

}
